<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <script type="text/javascript">
    function fn(url, callback) {
      var httpRequest;//创建XHR
      httpRequest = window.XMLHttpRequest ? new XMLHttpRequest() : window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : undefined;

      httpRequest.onreadystatechange = function() {
        if(httpRequest.readystate === 4 && httpRequest.status === 200){
          //状态判断
          callback.call(httpRequest.responseXML);
        }
      };
      httpRequest.open("GET", url);
      httpRequest.send();
    }

    // 读取xml，alert会阻碍代码执行
    fn("text.xml", function() {
      //调用函数，此语句后输出
      console.log(this);
    });

    console.log("this will run before the above callback.");//此语句先输出

    function a(callback) {
      callback();
    }
    function b() {
      alert("我是回调函数b");
    }
    function c() {
      alert("我是回调函数c");
    }
    function test() {
      // 参数传递的是回调函数，要执行的方法
      a(b);
      a(c);
    }
    test()
  </script>
</html>
